<?php

namespace App\Http\Controllers\Client\Common;

use App\Http\Controllers\ClientController;
use App\Models\Tools\Message;
use Illuminate\Support\Facades\Log;
use Illuminate\Support\Facades\Redis;

class SendMessageController extends ClientController
{
    public function index()
    {
        $all = $this->all;
        if(!isset($all['phone'])){
            return $this->setReturnJson(-1, '未传参数');
        }
        $phone = $all['phone'];

        try {
            // 生成六位数数字随机验证码
            $code = mt_rand(100000, 999999);
            // 存redis(hash,30分钟过期)
            Redis::hset('message_code_'.$this->we_id, 'phone', $phone);
            Redis::hset('message_code_'.$this->we_id, 'code', $code);
            Redis::expire('message_code_'.$this->we_id, 30*60);
            // 发送短信
            $result = Message::sendVerifyCode($phone, $code);
            if ($result) {
                return $this->setReturnJson(0, '短信发送成功');
            } else {
                return $this->setReturnJson(-1, '短信发送失败');
            }
        } catch (\Exception $e) {
            Log::error(SendMessageController::class, ['msg' => $e->getMessage(), 'file' => $e->getFile(), 'line' => $e->getLine()]);
            return $this->setReturnJson(-1, '短信发送失败');
        }
    }
}
